package com.woniu.smart.controller;

import com.woniu.smart.entity.Room;
import com.woniu.smart.service.RoomService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.NotRentedRoomVo;
import com.woniu.smart.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 晨星
 * @date 2022/10/21/21:19
 */
@RestController
@RequestMapping("rooms")
public class RoomController {
    
    @Autowired
    private RoomService roomService;

    /**
     * 查询全部房间
     *
     * @author 晨星
     * @date 2022/10/20 14:38
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("queryAll/{id}")
    public Result queryAllRoom(@PathVariable Long id){
        List<RoomVo> roomList = roomService.selectAll(id);
        return Result.ok().data("roomList",roomList);
    }

    /**
     * 查询所有未出租房间
     *
     * @author 晨星
     * @date 2022/10/31 10:28
     * @return java.util.List<com.woniu.smart.vo.NotRentedRoomVo>
     **/
    @PostMapping("queryAll")
    public Result selectAllNotRented(){
        List<NotRentedRoomVo> list = roomService.selectAllNotRented();
        return Result.ok().data("list",list);
    }

    /**
     * 根据id查询房间
     *
     * @author 晨星
     * @date 2022/10/20 21:40
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("{id}")
    public Result queryByRoomId(@PathVariable Long id){
        Room room = roomService.selectByPrimaryKey(id);
        return Result.ok().data("room",room);
    }

    /**
     * 新增房间的信息
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param room 房间的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("")
    public Result insertRoom(@RequestBody Room room){
        roomService.insertSelective(room);
        return Result.ok();
    }

    /**
     * 修改房间的信息
     *
     * @author 晨星
     * @date 2022/10/20 21:44
     * @param room 房间的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PutMapping("")
    public Result updateRoom(@RequestBody Room room){
        roomService.updateByPrimaryKeySelective(room);
        return Result.ok();
    }

    /**
     * 根据id删除房间
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @DeleteMapping("{id}")
    public Result deleteRoomById(@PathVariable Long id){
        roomService.deleteByPrimaryKey(id);
        return Result.ok();
    }
}
